package com.bestcem.xm.openplatform.grpc.client;

import cn.hutool.core.convert.Convert;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.openplatform.convert.grpc.OpenplatformGrpcConvertMapper;
import com.bestcem.xm.openplatform.grpc.client.dto.QdesProjectDto;
import com.bestcem.xm.openplatform.grpc.client.dto.QstructsDto;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.bestcem.xm.qdes.grpc.v1.services.ProjectStatus.*;

@Slf4j
@Component
public class QdesGrpcClient {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BaseQdesServiceGrpc.BaseQdesServiceBlockingStub baseStub;

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BpProjectServiceGrpc.BpProjectServiceBlockingStub bpProjectServiceBlockingStub;

    private final OpenplatformGrpcConvertMapper grpcConvertMapper;

    public QdesGrpcClient(OpenplatformGrpcConvertMapper grpcConvertMapper) {
        this.grpcConvertMapper = grpcConvertMapper;
    }

    public List<QdesProjectDto> listProject(String orgId) {
        if (StringUtils.isBlank(orgId)) {
            throw new IllegalArgumentException("orgId不能为空");
        }
        // 因貌似不支持search参数，所以将orgId和code组织到project中
        Project project = Project.newBuilder().setCompanyId(orgId).build();
        ListProjectRequest.Builder builder = ListProjectRequest.newBuilder();
        builder.setProject(project);
        builder.addAllStatus(Lists.newArrayList(PROJECT_STATUS_CREATED.getNumber(),
                PROJECT_STATUS_ACTIVATED.getNumber(),
                PROJECT_STATUS_FINISHED.getNumber()));
//        ListProjectRequest listProjectRequest = ListProjectRequest.newBuilder()
//                .setProject(project)
//                .setStatus(0, PROJECT_STATUS_CREATED.getNumber())
//                .setStatus(1, PROJECT_STATUS_ACTIVATED.getNumber())
//                .setStatus(2, PROJECT_STATUS_FINISHED.getNumber()).build();
        ProjectSearchResponse response = baseStub.listProject(builder.build());
        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
            log.error("[openplatform] listProject failed, response: {}", response);
            throw new IllegalArgumentException("orgId不能为空");
        }
        List<Project> projectListList = response.getProjectListList();
        List<QdesProjectDto> list = projectListList.stream()
                .filter(Objects::nonNull)
                .map(this::toQdesProjectDTO)
                .collect(Collectors.toList());
        return list;
    }

    public List<QstructsDto> listQStruct(String projectId, Object version) {
        ListQStructRequest.Builder builder = ListQStructRequest.newBuilder();
        QStruct.Builder qStruct = QStruct.newBuilder();
        qStruct.setProjectId(projectId);
        if (version instanceof Integer) {
            qStruct.setVersion((Integer) version);
        } else {
            builder.addAllVersions(Convert.toList(Integer.class, version));
        }
        builder.setQStruct(qStruct);
        QStructSearchResponse response = baseStub.listQStruct(builder.build());
        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
            log.error("[openplatform] listQStruct failed, response: {}", response);
            return null;
        }
        return response.getQStructListList().stream().map(grpcConvertMapper::toQstructsDto)
                .collect(Collectors.toList());
    }

    private QdesProjectDto toQdesProjectDTO(Project project) {
        QdesProjectDto qdesProjectDto = new QdesProjectDto();

        qdesProjectDto.setId(project.getId());
        qdesProjectDto.setCode(project.getCode());
        qdesProjectDto.setTitle(project.getTitle());
        qdesProjectDto.setCreator(project.getCreator());
        qdesProjectDto.setCompanyId(project.getCompanyId());
        qdesProjectDto.setGroupList(project.getGroupListList());
        qdesProjectDto.setVersion(project.getVersion());
        qdesProjectDto.setReleaseVer(project.getReleaseVer());
        qdesProjectDto.setStatus(project.getStatusValue());
        qdesProjectDto.setStructId(project.getStructId());
        qdesProjectDto.setLogicId(project.getLogicId());
        qdesProjectDto.setCustomAttr(project.getCustomAttr());
        qdesProjectDto.setSource(project.getSource());
        qdesProjectDto.setExtId(project.getExtId());
        qdesProjectDto.setTimerExpire(project.getTimerExpire());
        qdesProjectDto.setType(project.getPtypeValue());
        qdesProjectDto.setCopyDemo(project.getIsCopyDemo());
        return qdesProjectDto;
    }

}
